package com.vois.jack.btmgr.blebase;

import android.bluetooth.BluetoothDevice;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BleDeviceBuilder {
    static private Map<String, BleDeviceInfo> bleNameDeviceMap = new HashMap<>();
    static private List<String> bleRegisteredNameList = new ArrayList<>();

    static class BleDeviceInfo {
        Class<? extends BleDevice> bleClass;
        int burstSrcType;

        public BleDeviceInfo(Class<? extends BleDevice> bleClass, int burstSrcType) {
            this.bleClass = bleClass;
            this.burstSrcType = burstSrcType;
        }
    }

    static public void registerBleDeviceClass(String name, Class<? extends BleDevice> bleClass, int burstSrcType, boolean presentInScan) {
        if (presentInScan) {
            bleRegisteredNameList.add(name);
        }
        bleNameDeviceMap.put(name, new BleDeviceInfo(bleClass, burstSrcType));
    }

    static public String getNameFilterString() {
        StringBuffer stringBuffer = new StringBuffer();
        for (String name: bleRegisteredNameList) {
            stringBuffer.append(name).append("|");
        }

        stringBuffer.deleteCharAt(stringBuffer.length() - 1);
        return stringBuffer.toString();
    }

    static public int getBleDeviceSrcType(BluetoothDevice device) {
        Set<String> keys = bleNameDeviceMap.keySet();
        String name = device.getName();
        if (name != null) {
            for (String key : keys) {
                Pattern pattern = Pattern.compile(key);
                Matcher matcher = pattern.matcher(name);

                if (matcher.find()) {
                    return bleNameDeviceMap.get(key).burstSrcType;
                }
            }
        }

        return 0;
    }

    static public BleDevice buildBleDevice(BluetoothDevice device, String deviceName) {
        Set<String> keys = bleNameDeviceMap.keySet();
        Class<? extends BleDevice> cls = null;
        BleDevice bleDevice = null;

        String keyName = null;
        String compareName = deviceName != null ? deviceName: device.getName();

        if (compareName != null) {
            for (String key : keys) {
                Pattern pattern = Pattern.compile(key);
                Matcher matcher = pattern.matcher(compareName);

                if (matcher.find()) {
                    keyName = key;
                    break;
                }
            }
        }

        if (keyName != null) {
            cls = bleNameDeviceMap.get(keyName).bleClass;
        }

        if (cls == null) {
            cls = DefaultBleDevice.class;
        }

        try {
            bleDevice = cls.newInstance();
            bleDevice.init(device, compareName);
        }catch (IllegalAccessException | InstantiationException e) {
            e.printStackTrace();
        }

        return bleDevice;
    }
}
